﻿#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include <termios.h>
#include <stdlib.h>
//#include <termios.h>
#include <sys/ioctl.h>

#include "fore_rtdb.h" 
#include "rtdbFun.h"
using namespace std;
void CreateRtdb(); 
void printf_yc_rtdb( ); 
 
void CreateRtdb()
{
	EmptyAll(); 

	printf("CreateRtdb begin.....\n");
 
	CreateRTdb( "SYS_STRING_KEY_NAME",				SYS_STRING_KEY_NO,		SYS_STRING_KEY_COUNT,		sizeof(SYS_STRING_KEY));
	 
	CreateRTdb( "FORE_YC_POINT_RTDB_NAME",			FORE_YC_POINT_NO,		FORE_YC_POINT_COUNT,		sizeof(FORE_YC_POINT)); 
	 	
	printf("CreateRtdb end.....\n");
}


int main(int argc, char *argv[])
{ 

	printf( "\n--------rtdb_printf enter!!!\n" );
  	   
	sleep(2);
	
	//打开数据库
	if( !OpenDbms() ) {
		printf("msgSvr: open rtdbms error! \n");  
		return -1;
	}
	else{
		printf("open rtdbms sucess!\n"); 
	}  
	
	//判断实时库是否被创建过
	if( !IsLibNoValid(SYS_STRING_KEY_NO) || !IsLibNoValid(FORE_YC_POINT_NO) )
	{
		CreateRtdb();
		
		SaveRtdb();
		
		printf("Load rtdb rtdbms\n");
	}
	else {
		
		if( !LoadRtdb() ) 
		{
			printf(" Load rtdb rtdbms failed2! \n"); 
			return false;
		}
		else
		{
			printf(" Load rtdb rtdbms sucess2! \n"); 
		} 
	}
	
	sleep(2); 
	//插入记录（下面有savertdb 所以这里这调用一次就行，即第二次运行程序时，LoadRtdb函数会自动装载上次运行插入的记录，这里需要注意，通常这里插入记录就插入一次就行， ）
	char  key[64]={0};
	char nodeId[32] ={0};
	char meaName[32]={0};
	for( int i=0; i<100; i++ )
	{
		sprintf(nodeId, "GUID_%03d", i+1 );
		sprintf(meaName, "meaName%d", i+1 );  //对应xml中的  PhV_phsA、PhV_phsB等
		sprintf(key, "%s:%s", nodeId, meaName );
		
		FORE_YC_POINT   yc_rec;
		strcpy(yc_rec.dev_nodeID, nodeId);
		strcpy(yc_rec.name, meaName);
		
		UpdateRec_stringkey_Ex(FORE_YC_POINT_NO, nodeId, &yc_rec, sizeof(FORE_YC_POINT));
	}
	SaveRtdb();
	
	// 打印插入记录
	sleep(2); 
	
	printf_yc_rtdb( );
	
	
	//更新插入记录值
	strcpy(key, "GUID_010:meaName10" );
	FORE_YC_POINT * pRec = FindRec_stringkey(FORE_YC_POINT_NO, key);
	if( pRec ) {
		pRec->CurYcValue = 221.24;
		pRec->LastUpdateTime = time(NULL);
	}
		
	// 打印更新记录的采集值
	//.....
	
	
	return 0;
}
 

int qsort_yc_point( const void *a , const void *b )
{
	FORE_YC_POINT * p1 = (FORE_YC_POINT*)a;
	FORE_YC_POINT * p2 = (FORE_YC_POINT*)b; 
 
 
	if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) > 0 ) 
		return 1;
	else if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) < 0 ) 
		return -1;
	else 
	{
		if( strcmp( p1->name, p2->name ) > 0 ) 
			return 1;
		else if( strcmp( p1->name, p2->name) < 0 ) 
			return -1;
		else return 0;
	}
	 
}
 
void printf_yc_rtdb( )
{
	printf("\n\n -----------------printf_yc_rtdb--------- \n\n");

	tHandle handle = OpenLib(FORE_YC_POINT_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_YC_POINT_NO is InValid \n");
		return ;
	}
	
	std::vector<FORE_YC_POINT>  aryYc; 

	FORE_YC_POINT yc_rec;
	bool ret = ReadFirst(handle, (char*)&yc_rec, sizeof(FORE_YC_POINT ));

	while( ret )
	{  
			
		aryYc.push_back( yc_rec );  
		 
		ret = ReadNext(handle, (char*)&yc_rec, sizeof(FORE_YC_POINT ));
	}
	
	CloseLib( handle );
	
	if( aryYc.size() > 0 )
		qsort(aryYc.data(), aryYc.size(), sizeof(FORE_YC_POINT), qsort_yc_point );

	for( int i=0; i<aryYc.size(); i++ )
	{
		FORE_YC_POINT & yc_rec = aryYc[i];
		
		printf("key=%lld, dev_nodeID=%s, name=%s, unitSymbol=%s, time=%d, val = %0.3f, appName=%s \n",  yc_rec.KEY, yc_rec.dev_nodeID, yc_rec.name, yc_rec.unitSymbol,    yc_rec.LastUpdateTime, yc_rec.CurYcValue, yc_rec.appName);
	}
	 
	printf(" \n \n\n  count = %d  \n", aryYc.size() );
	
}
 
 


